#include "localtextplay.h"
#include "mainwindow.h"
#include "textplayer.h"
#include "subwindowplaytextfunctor.h"
#include "common.h"
#include "pathconst.h"

#include <QDomDocument>
#include <QFile>
#include <QDebug>

LocalTextPlay::LocalTextPlay(int windowNo, const QString &text):
		m_WindowNo(windowNo)
		,m_Text(text)
{
}

void LocalTextPlay::doCommand()
{

	QDomDocument domDoc;
	QDomElement rootElement = domDoc.createElement(g_LocalTextTagName);
	domDoc.appendChild(rootElement);
	rootElement.setAttribute(g_LocalTextAttribute, m_Text);

	QString playListPath = PlayListPath(m_WindowNo);
	QFile domDocFile(playListPath);
	if(!domDocFile.open(QFile::WriteOnly))
	{
		QString errorMsg = QString("Open local text file %1 for write error").arg(playListPath);
		qDebug()<<errorMsg;
		return;
	}
	QTextStream stream(&domDocFile);
	domDoc.save(stream, 4);
	domDocFile.close();

	SubWindowPlayTextFunctor textPlay(4, m_Text);
	textPlay();
}
